package com.wzz.second.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

@RestController
@RequestMapping("/activeMq")
public class ProviderController {

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @GetMapping("/send")
    public void send(String name){
        //添加消息到消息队列
        jmsMessagingTemplate.convertAndSend(queue,name);
    }

    @JmsListener(destination = "${myQueueName}")
    @SendTo("SQueue")
    public String handleMessage(String name){
        System.out.println("接收的数据"+name);
        return "接受的"+name;
    }

    @GetMapping("/sendTopic")
    public void sendTopic(String name){
        jmsMessagingTemplate.convertAndSend(topic,name);

    }
    @GetMapping("/sendTopicTwo")
    public void sendTopicTwo(String name){
        jmsMessagingTemplate.convertAndSend(topic,name);
    }
    @JmsListener(destination = "${myTopicName}")
    @SendTo("STopic")
    public String subMessage(String name){
        System.out.println("接收的数据"+name);
        return name;
    }

    @JmsListener(destination = "${myTopicName}")
    @SendTo("STopic")
    public String subMessageT(String name){
        System.out.println("第二个订阅者"+name);
        return name;
    }
}
